<template>
  <div class="login">
    <div class="main">
      <h2 class="title">微心科技智能OA系统®</h2>
      <el-form
        ref="loginFormRef"
        v-loading="loading"
        element-loading-text="正在登录..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        class="login-form"
        :model="loginForm"
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            prefix-icon="iconfont icon-yonghu"
            auto-complete="off"
            placeholder="账号"
          />
        </el-form-item>
        <el-form-item prop="password" v-if="visiblePwd">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-mima"
            auto-complete="off"
            placeholder="密码"
          >
            <i
              slot="suffix"
              title="显示密码"
              style="cursor:pointer;"
              class="iconfont icon-yanjing_yincang"
              @click="changePass('show')"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" v-else>
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
            auto-complete="off"
            placeholder="密码"
          >
            <i
              slot="suffix"
              title="隐藏密码"
              style="cursor:pointer;"
              class="iconfont icon-yanjing_xianshi"
              @click="changePass('hide')"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            prefix-icon="iconfont icon-yanzhengma"
            auto-complete="off"
            placeholder="验证码"
            style="width: 58%"
          />
          <div class="login-code">
            <img :src="captchaUrl" class="login-code-img" @click="updateCaptcha">
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            size="medium"
            type="primary"
            style="width:100%;"
            @click="submitLogin"
          >登 录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2019-2022 零九科技 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
  // 导入阿里巴巴矢量图样式表
  import '../../assets/fonts/iconfont.css'
  export default {
    name: 'Login',
    data() {
      // 这里存放数据
      return {
        // 这里请求后端接口/captcha来获取验证码，另外还要带上时间参数，为什么要带上时间参数。
        // 因为如果验证码看不清，需要手动点击更换验证码，如果不加时间参数，在点击时获取到的还是同一张验证码。
        // 它会认为你是调用同一个接口，传的是同一个参数，就会把上次图形进行返回。加上时间参数会确保能够请求到
        // 新的验证码图片。
        captchaUrl: '/captcha?time=' + new Date(),
        loginForm: {
          username: '', // 用户名
          password: '', // 密码
          rememberMe: true, // 记住我
          code: '' // 验证码
        },
        visiblePwd: true,
        loginRules: {
          username: [
            { required: true, trigger: 'blur', message: '用户名不能为空' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, trigger: 'blur', message: '密码不能为空' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          code: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
        },
        // false表示不是正在加载中，true表示正在加载中
        loading: false
      }
    },
    watch: {
    },
    methods: {
      // 显示密码
      changePass(value) {
        this.visiblePwd = !(value === 'show')
      },
      // 验证码图片的点击事件，实现更新验证码图片
      updateCaptcha() {
        this.captchaUrl = '/captcha?time=' + new Date()
      },
      // 登录提交
      submitLogin() {
        this.$refs.loginFormRef.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              // {path: this.redirect || '/'} 这种是函数参数默认值，判断this.redirect是否为空
              // 如果是空的，给默认值 '/'
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            }).catch((error) => {
              console.log(error)
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    /* height: 705px;*/
    background: url("../../assets/images/login-background.jpg") no-repeat;
    background-size: 100% 100%;
    position: relative;
  }
  .main {
    position: absolute;
    height: 50%;
    top: 19%;
    right: 10%;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #fff;
  }

  .login-form {
    background-image: url("../../assets/images/background_login.png");
    background-size: 100% 100%;
    border-radius: 6px;
    width: 340px;
    padding: 45px;
  }
  .login-form .el-checkbox {
    color: #fff;
    float: left;
    padding-left: 4px;
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .login-form .el-input {
    height: 38px;
  }
  .login-form .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: gray;
    cursor: pointer;
    user-select: none;
  }
  // .login-form input {
  /*使用/deep/ ，两边要加空格，来做样式穿透，来提高此样式的优先级别*/
  .el-input /deep/ .el-input__inner{
    background: #597277;
    height: 38px;
    color: #fff;
  }
  .login-code {
    width: 39%;
    height: 38px;
    float: right;
  }
  .login-code img {
    cursor: pointer;
    vertical-align: middle;
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 38px;
  }
</style>

